﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wpb
{
    public partial class Form2 : Form
    {

        //static变量
        public static string strIP = "";
        public static string strPort = "";
        public static bool isProxy = false;

        public Form2()
        {
            InitializeComponent();
        }

        //open username.txt
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openUsername = new OpenFileDialog();
            openUsername.InitialDirectory = System.Environment.CurrentDirectory;
            openUsername.Filter = "文本文件|*.txt|所有文件|*.*";
            if (openUsername.ShowDialog() == DialogResult.OK)
            {
                string pathUsername = openUsername.FileName; ;
                textBox1.Text = pathUsername;
                StreamReader readUsername = new StreamReader(pathUsername, Encoding.Default);
                try
                {
                    this.richTextBox1.Text = readUsername.ReadToEnd();
                }
                catch
                {
                    MessageBox.Show("Read File Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    readUsername.Close();
                }
            }
        }

        //open password.txt
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPassword = new OpenFileDialog();
            openPassword.InitialDirectory = System.Environment.CurrentDirectory;
            openPassword.Filter = "文本文件|*.txt|所有文件|*.*";
            if (openPassword.ShowDialog() == DialogResult.OK)
            {
                string pathPassword = openPassword.FileName; ;
                textBox2.Text = pathPassword;
                StreamReader readPassword = new StreamReader(pathPassword, Encoding.Default);
                try
                {
                    this.richTextBox2.Text = readPassword.ReadToEnd();
                }
                catch
                {
                    MessageBox.Show("Read File Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    readPassword.Close();
                }
            }
        }

        //open url.txt
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openUrl = new OpenFileDialog();
            openUrl.InitialDirectory = System.Environment.CurrentDirectory;
            openUrl.Title = "选择登录后台地址文件";
            openUrl.Filter = "文本文件|*.txt|所有文件|*.*";
            if (openUrl.ShowDialog() == DialogResult.OK)
            {
                string pathUrl = openUrl.FileName; ;
                textBox3.Text = pathUrl;
                StreamReader readUrl = new StreamReader(pathUrl, Encoding.Default);
                try
                {
                    this.richTextBox3.Text = readUrl.ReadToEnd();
                }
                catch
                {
                    MessageBox.Show("Read File Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    readUrl.Close();
                }
            }
        }

        //make
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string strPath = System.Environment.CurrentDirectory;
                FileStream fsUsername = new FileStream("username", FileMode.OpenOrCreate);
                StreamWriter swUsername = new StreamWriter(fsUsername);
                swUsername.Write(this.richTextBox1.Text);
                swUsername.Flush();
                swUsername.Close();
                fsUsername.Close();

                FileStream fsPassword = new FileStream("password", FileMode.OpenOrCreate);
                StreamWriter swPassword = new StreamWriter(fsPassword);
                swPassword.Write(this.richTextBox2.Text);
                swPassword.Flush();
                swPassword.Close();
                fsPassword.Close();

                FileStream fsUrl = new FileStream("url", FileMode.OpenOrCreate);
                StreamWriter swUrl = new StreamWriter(fsUrl);
                swUrl.Write(this.richTextBox3.Text);
                swUrl.Flush();
                swUrl.Close();
                fsUrl.Close();

                strIP = textBox5.Text;
                strPort = textBox6.Text;

                MessageBox.Show("设置成功","关闭该窗口即可！");
            }
            catch(Exception Err)
            {
                MessageBox.Show("设置失败",Err.Message);
            }
            
        }

        //reset
        private void button6_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";
            this.richTextBox2.Text = "";
            this.richTextBox3.Text = "";
            this.textBox5.Text = "127.0.0.1";
            this.textBox6.Text = "8080";
        }

        private void textBox5_Enter(object sender, EventArgs e)
        {
            this.textBox5.Text = "";
        }

        private void textBox5_Leave(object sender, EventArgs e)
        {
            if(this.textBox5.Text == "")
            {
                this.textBox5.Text = "127.0.0.1";
            }
        }

        private void textBox6_Enter(object sender, EventArgs e)
        {
            this.textBox6.Text = "";
        }

        private void textBox6_Leave(object sender, EventArgs e)
        {
            if (this.textBox6.Text == "")
            {
                this.textBox6.Text = "127.0.0.1";
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                isProxy = true;
                textBox5.Enabled = true;
                textBox6.Enabled = true;
            }
            else
            {
                isProxy = false;
                textBox5.Enabled = false;
                textBox6.Enabled = false;
            }
        }


    }
}
